# __author__ = 'heyin'
# __date__ = '2018/11/15 15:15'
import tushare as ts
import pandas as pd
import os

d = ts.get_hist_data('sh')


# 从中取出特征值和目标值
x = d[['open', 'high', 'close', 'low', 'volume', 'p_change', 'ma5', 'ma10', 'ma20']]
p_change = d[['p_change']]

# print(d)

x = x.iloc[1:, :]  # 删掉最新一天
x.reset_index(inplace=True)
# x.pop('date')
print(x)

p_change[p_change > 0] = 1
p_change[p_change < 0] = 0
p_change = p_change.iloc[:-1, :]
p_change.reset_index(inplace=True)
p_change.pop('date')
print(p_change)

# 合并数据
x['up_down'] = p_change
print(x)

# 创建存储股票的文件夹
if not os.path.exists('./stockdata/'):
    os.mkdir('./stockdata')
file_path = './stockdata/{}.csv'.format('sh')  # 注意文件名不能是中文，否则使用pd读取的时候会报错，提示无法初始化，OS异常
x.to_csv(file_path, index=False)
print('数据已写入 {} 文件内'.format(file_path))

